// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let {
    op,
    _id
  } = event
  switch (op) {
    case 'getUsers':
      return getUsers()
      break;
    case 'delUser':
      return delUser()
      break;
  }
  async function getUsers() {
    const countResult = await db.collection('User').count()
    const total = countResult.total
    let res = await db.collection('User').orderBy("register_time", "desc").orderBy('_id', 'desc').get()
    return {
      total: total,
      list: res.data  
  }
}
  async function delUser() {
 
    let res = await db.collection('User').doc(_id).remove()
    return res

  }


}